"use strict";



function RunCommands(pid: number) {
	var ctl = host.namespace.Debugger.Utility.Control;
	var output = ctl.ExecuteCommand("k");
	displayOutput(output);
	host.diagnostics.debugLog("***> Exiting RunCommands Function \n");
}

function displayOutput(output) {
	for (var line of output) {
		host.diagnostics.debugLog("  ", line, "\n");
	}
}

function PreFunc(pid: number) {
	var ctl = host.namespace.Debugger.Utility.Control;
	var output = ctl.ExecuteCommand("!process 0n" + pid + " 0");
	host.diagnostics.debugLog("***> Displaying command output " + pid + "\n");
	displayOutput(output);
	for (var line of output) {

		if (/PROCESS(\s{1})(\S+)/.test(line)) {
			var procAddr = /PROCESS(\s{1})(\S+)/.exec(line)[2];
			host.diagnostics.debugLog("Found PROCESS Address:=> " + procAddr + "\n");
			output = ctl.ExecuteCommand(".process /i /p " + procAddr + ";g;");
			displayOutput(output);
			output = ctl.ExecuteCommand(".process /r /p " + procAddr + ";");
			displayOutput(output);
		}
	}
	host.diagnostics.debugLog("***> Exiting RunCommands Function \n");
}

function pn(pid: number) {
	PreFunc(pid);
	var ctl = host.namespace.Debugger.Utility.Control;
	var output = ctl.ExecuteCommand("lm;");
	displayOutput(output);
}

function p(pid: number) {
	PreFunc(pid);
	var ctl = host.namespace.Debugger.Utility.Control;
	var output = ctl.ExecuteCommand(".reload /f win32k.sys;lm;");
	displayOutput(output);
}


// TitleFinder.js
// A function which uses just JavaScript concepts to find the title of an application
//
function findTitle() {
	var curProcess = host.currentProcess;
	for (var thread of curProcess.Threads) {
		for (var frame of thread.Stack.Frames) {
			if (frame.toString().includes("__mainCRTStartup")) {
				var locals = frame.LocalVariables;

				//
				// locals.StartupInfo.lpTitle is just an unsigned short *.  We need to actually call an API to
				// read the UTF-16 string in the target address space.  This would be true even if this were
				// a char* or wchar_t*.
				//
				return host.memory.readWideString(locals.StartupInfo.lpTitle);
			}
		}
	}
}

function __constants() {
	return {math_constants: {pi: 3.1415926535, e: 2.7182818284}, description: "Archimedes' pi and Euler's number e"};
}


//
// A function which uses both JavaScript and integrated LINQ concepts to do the same.
//
function findTitleWithLINQ() {
	var isMainFrame = function (frame) {
		return frame.toString().includes("Main");
	};
	var isMainThread = function (thread) {
		return thread.Stack.Frames.Any(isMainFrame);
	};

	var curProcess = host.currentProcess;
	var mainThread = curProcess.Threads.Where(isMainThread).First();
	var mainFrame = mainThread.Stack.Frames.Where(isMainFrame).First();

	var locals = mainFrame.Parameters;

	host.diagnostics.debugLog("mainFrame LocalVariables :=> " + JSON.stringify(mainFrame) + "\n");

	/**

	 // locals.StartupInfo.lpTitle is just an unsigned short *.  We need to actually call an API to
	 // read the UTF-16 string in the target address space.  This would be true even if this were
	 // a char* or wchar_t*.
	 */
	for (var namew of locals) {
		/*	return host.memory.readWideString(name);*/
		host.diagnostics.debugLog("Found LocalVariables :=> " + JSON.stringify(namew) + "\n");
	}

}


function one1() {
	return new host.Int64(1);
}

function initializeScript() {
	return [new host.functionAlias(__constants, "constants")];
}